<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Areas_model extends MY_Model {

	var $table = 'areas_enabled';

	//构造函数
	public function __construct() {
		//调用父类构造函数，必不可少
		parent::__construct();
	}

	/**
	 * 取得所有启用网点仓库
	 * 
	 * @param array $config
	 * @param array $like
	 * @return array
	 */
	public function getAreasAll($config = array(), $like = array(), $where = array()) {
		$config['totalCount'] = $this->get_search_num($where, $like, $config, ' ae');
		if (isset($config['offset']) && $config['offset'] >= $config['totalCount']) {
			$config['offset'] = 0;
			$config['pageNum'] = 1;
		}
		$data = $config;
		$config['select'] = 'ae.id,ae.address,ae.status,ae.type,ae.created_at,a.description';
		$config['join'][] = array('areas as a', 'ae.district_id = a.id', 'left');
		$data['data'] = $this->search($where, $config, $like, true, ' ae');
		return $data;
	}

	/**
	 * 增加启用地区
	 * 
	 * @param array $data
	 * @return integer
	 */
	public function addAreas($data) {
		$this->db->trans_start();
		$warehouse_address_id = $data['warehouse_address_id'];
		unset($data['warehouse_address_id']);
		$this->db->insert($this->table, $data);
		$id = $this->db->insert_id();
		//仓库上必有一个网点
		if($data['type'] === '2') {
			$this->db->insert('branch_assignment', array('address_id'=>$id,'warehouse_address_id'=>$id,'created_at'=>time()));
		} else {
			$this->db->insert('branch_assignment', array('address_id'=>$id,'warehouse_address_id'=>$warehouse_address_id,'created_at'=>time()));
		}
		$this->db->trans_complete();
		return $this->db->trans_status();
	}

	/**
	 * 删除启用地区
	 * 
	 * @param string $ids 多个用逗号隔开
	 * @return integer
	 */
	public function deleteAreas($ids) {
		$idsArr = explode(',', $ids);
		return $this->delete(array('id' => $idsArr));
	}

	/**
	 * 更新启用地区
	 * 
	 * @param array $data
	 * @param integer $id
	 * @return integer
	 */
	public function updateAreas($data, $id) {
		return $this->update($data, array('id' => $id));
	}

	/**
	 * 查找详细信息
	 * 
	 * @param integer $id
	 * @return array
	 */
	public function findAreas($id) {
		return $this->get_row(array('id' => $id));
	}

	/**
	 * 搜索地区级联表
	 * 
	 * @param integer $parent
	 * @return array
	 */
	public function searchAreas($parent) {
		$query = $this->db->select('id,name')->from('areas')->where(array('parent' => $parent, 'status' => REST_Controller::STATUS_ACTIVE))->get();
		$result = $query->result_array();
		$data = array();
		if (!empty($result)) {
			foreach ($result as $key => $val) {
				$data[] = array($val['id'], $val['name']);
			}
		}
		return $data;
	}

	/**
	 * 地区是否添加
	 * 
	 * @param array $data
	 * @return boolean
	 */
	public function is_unique($data) {
		$num = $this->get_num(array('address' => $data['address'], 'district_id' => $data['district_id']));
		return empty($num)? true : false;
	}
	
	
	/**
	 * 取得所有启用地区(只取仓库id和名字)
	 * @return array
	 */
	public function getAreasIN() {
		$data = array();
		$result = $this->get_result(null, array('select'=>'id,address'));
		if(!empty($result)) {
			foreach ($result as $v) {
				$data[$v['id']] = $v['address'];
			}
		}
		return $data;
	}

}
